package gitee.derumin.really_forge.mixin;

import com.google.common.collect.Multimap;
import gitee.derumin.really_forge.enchantments.CanChangeAttributeModifier;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin {

    @Inject(method = "getAttributeModifiers", at = @At("RETURN"), cancellable = true)
    private void applyEnchantment(EquipmentSlot slot, CallbackInfoReturnable<Multimap<EntityAttribute, EntityAttributeModifier>> cir){
        EnchantmentHelper.get((ItemStack)(Object) this).forEach(((enchantment, integer) -> {
            if (enchantment instanceof CanChangeAttributeModifier changer){
                cir.setReturnValue(changer.changeAttributeModifiers(cir.getReturnValue(), integer));
            }
        }));
    }
}
